package team.cdlzz.service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.cdlzz.dao.CourseDetailMapper;
import team.cdlzz.dao.CourseMapper;
import team.cdlzz.dao.SCMapper;
import team.cdlzz.dao.UserMapper;
import team.cdlzz.model.pojo.CourseDetail;
import team.cdlzz.model.pojo.User;
import team.cdlzz.util.IDUtils;

import java.util.*;

/**
 * @author zw
 * @date 2022-06-22.
 */
@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UtilService utilService;
    @Autowired
    SCMapper scMapper;
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    CourseDetailMapper courseDetailMapper;

    public User login_u(Integer uid, String pass) {
        User user = userMapper.selectByPrimaryKey(uid);
        if (user != null && user.getPwd().equals(pass)) {
            user.setPhoto(IDUtils.getPath(user.getPhoto(), 1));
            return user;
        } else
            return null;
    }

    public User getUser(Integer uid) {
        User user = userMapper.selectByPrimaryKey(uid);
        if (user != null)
            user.setPhoto(IDUtils.getPath(user.getPhoto(), 1));
        return user;
    }

    public User login(String email, String pass) {
        User user = userMapper.login(email);
        if (user != null && user.getPwd().equals(pass)) {
            user.setPhoto(IDUtils.getPath(user.getPhoto(), 1));
            return user;
        } else
            return null;
    }

    /**
     * 传入页数以及单页页面数量值,返回对应的所有的分页数据
     *
     * @param page
     * @param pageSize
     * @return
     */
    public List findPage(int page, int pageSize) {
//        System.out.println(page+"  ===  "+pageSize);
        PageHelper.startPage(page, pageSize);
        List<User> list = userMapper.selectAll();
        list = setImg(list);
        PageInfo pageInfo = new PageInfo<>(list);
        return pageInfo.getList();
    }

    public String UserRegister(String email, String pass, String verify) {
        String s = UtilService.mailString.get(email);
        System.out.println(s + "===" + verify);
        if (s != null) {
            if (s.equals(verify)) {
                User user = new User();
                user.setEmail(email);
                user.setPwd(pass);
                Random random = new Random();
                int s1 = random.nextInt();
                user.setName("用户" + s1);
                System.out.println(s1);
                int i = userMapper.insert(user);
                if (i == 1)
                    return "true";
                else
                    return "出现了未知错误";
            } else
                return "验证码错误";
        }
        return "该邮箱未发送验证码";
    }

    public JSONObject getUserInfo(Integer uid) {
        JSONObject jsonObject = new JSONObject();
        List<Map<String , Object>> selectCourse = getSelectCourse(uid);
        List<Map<String , Object>> createCourse = getCreateCourse(uid);
        User user = getUser(uid);
        jsonObject.put("user", user);
        jsonObject.put("create", createCourse);
        jsonObject.put("select", selectCourse);
        return jsonObject;
    }

    /**
     * 获取到用户创建的课程集合
     * @param uid
     * @return
     */
    public  List<Map<String , Object>> getCreateCourse(Integer uid){
        List<Integer> uidCreateCount = courseMapper.UidCreateCount(uid); //获取到用户创建的课程
        return CidListByCourseDetails(uidCreateCount);
    }

    /**
     * 获取到用户选择的课程集合
     *
     * @param uid
     * @return
     */
    public  List<Map<String , Object>> getSelectCourse(Integer uid){
        List<Integer> uidSelectCount = scMapper.uidSelectCount(uid); //获取到用户创建的课程
        return CidListByCourseDetails(uidSelectCount);
    }

    /**
     * 传入课程集合,返回list集合
     * @param uidSelectCount
     * @return
     */
    private List<Map<String , Object>>CidListByCourseDetails(List<Integer> uidSelectCount) {
        List<Map<String , Object>> list = new ArrayList<>();
        for (Integer s : uidSelectCount) {
            Map<String , Object> courseListInfo = new HashMap<>();
            CourseDetail courseDetail = courseDetailMapper.selectByPrimaryKey(s);
            Integer integer =0;
            if (courseDetail!=null){
                courseDetail.setcImg(IDUtils.getPath(courseDetail.getcImg(), 2));//添加图片
                integer = courseMapper.selectCount(courseDetail.getCid());
                courseListInfo.put("count",integer);
                courseListInfo.put("courseInfo",courseDetail);
                list.add(courseListInfo);
            }
        }
        return list;
    }

    /**
     * 为用户添加头像图片
     *
     * @param list
     * @return
     */
    public List<User> setImg(List<User> list) {
        for (User user : list) {
            user.setPhoto(IDUtils.getPath(user.getPhoto(), 1));
        }
        return list;
    }

    @Transactional //升级为事务
    public String userUpdata(User user) {
        int insert = userMapper.updateByPrimaryKeySelective(user);
        if (insert==1){
            return "修改成功";
        }
        else {
            return "修改失败--值为发生改变?";
        }

    }
}